package com.ruoyi.cms.controller;

import com.ruoyi.common.utils.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cms.domain.CmsModel;
import com.ruoyi.cms.domain.dto.CmsModelDTO;
import com.ruoyi.cms.service.ICmsModelService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.HTExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 数据模型Controller
 * 
 * @author ruoyi
 * @date 2021-07-29
 */
@Api(tags = "数据模型")
@Controller
@RequestMapping("/cms/model")
public class CmsModelController extends BaseController {

    private final String prefix = "cms/model";

    @Autowired
    private ICmsModelService cmsModelService;

    @RequiresPermissions("cms:model:view")
    @GetMapping()
    public String model() {
        return prefix + "/model";
    }

    /**
     * 查询数据模型列表
     */
    @ApiOperation("查询数据模型列表")
    @RequiresPermissions("cms:model:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo<CmsModel> list(CmsModelDTO cmsModel) {
        startPage();
        List<CmsModel> list = cmsModelService.selectCmsModelList(cmsModel.as());
        return getDataTable(list);
    }

    /**
     * 导出数据模型列表
     */
    @ApiOperation(value="导出数据模型列表", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    @RequiresPermissions("cms:model:export")
    @Log(title = "数据模型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult<?> export(CmsModelDTO cmsModel) {
        List<CmsModel> list = cmsModelService.selectCmsModelList(cmsModel.as());
        return HTExcelUtil.exportExcel(CmsModel.class, list, queryVisibleFields());
    }

    /**
     * 新增数据模型
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存数据模型
     */
    @ApiOperation("新增保存数据模型")
    @RequiresPermissions("cms:model:add")
    @Log(title = "数据模型", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult<?> addSave(@Validated CmsModelDTO cmsModel) {
        cmsModel.setModelId(null);
        cmsModel.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(cmsModelService.insertCmsModel(cmsModel.as()));
    }

    /**
     * 修改数据模型
     */
    @GetMapping("/edit/{modelId}")
    public String edit(@PathVariable("modelId") Long modelId, ModelMap mmap)
    {
        CmsModel cmsModel = cmsModelService.selectCmsModelById(modelId);
        mmap.put("cmsModel", cmsModel);
        return prefix + "/edit";
    }

    /**
     * 修改保存数据模型
     */
    @ApiOperation("修改保存数据模型")
    @RequiresPermissions("cms:model:edit")
    @Log(title = "数据模型", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult<?> editSave(@Validated CmsModelDTO cmsModel) {
        if (null == cmsModel.getModelId()) {
            return AjaxResult.error("ID不能为空");
        }
        cmsModel.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(cmsModelService.updateCmsModel(cmsModel.as()));
    }

    /**
     * 删除数据模型
     */
    @ApiOperation("删除数据模型")
    @ApiImplicitParam(name = "ids", value = "数据模型ID.多个ID间以英文逗号隔开", dataType = "String", paramType = "query", required = true)
    @RequiresPermissions("cms:model:remove")
    @Log(title = "数据模型", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult<?> remove(String ids) {
        return toAjax(cmsModelService.deleteCmsModelByIds(ids));
    }
}
